package com.naiterui.ehp.ps.form.mongo;

import java.util.List;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.naiterui.ehp.ps.form.bean.FormData;

import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class FormDataDao extends MongoBaseDao {

    private static final String COLLECTION_NAME = "form_data";
    private final MongoTemplate mongoTemplate;

    public void save(FormData formData) {
        super.save(formData, COLLECTION_NAME);
    }

    public FormData getFormData(String dataId) {
        Query query = new Query();
        query.addCriteria(new Criteria("_id").is(dataId));
        List<FormData> list = super.find(query, FormData.class, COLLECTION_NAME);
        return list.size() > 0 ? list.get(0) : null;
    }

    public List<FormData> getFormData(String formId, String businessId) {
        Query query = new Query();
        query.addCriteria(new Criteria("formId").is(formId));
        query.addCriteria(new Criteria("businessId").is(businessId));
        List<FormData> list = super.find(query, FormData.class, COLLECTION_NAME);
        return list;
    }

    public boolean existFormIdAndBusinessId(String formId, String businessId) {
        Query query = new Query();
        query.addCriteria(new Criteria("formId").is(formId));
        query.addCriteria(new Criteria("businessId").is(businessId));
        List<FormData> list = super.find(query, FormData.class, COLLECTION_NAME);
        return !CollectionUtils.isEmpty(list);
    }

    @Override
    protected MongoTemplate getMongoTemplate() {
        return this.mongoTemplate;
    }

}
